package org.dtrd.modules.evaluation.controller;

import cn.hutool.core.util.ArrayUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.common.api.vo.Result;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.evaluation.entity.bean.DiseaseEvaluationInfo;
import org.dtrd.modules.evaluation.entity.dto.DiseaseDTO;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdRetinopathyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;

import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_ADD;
import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_QUERY;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_ERROR;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * @Author: QR
 * @Date: 2021/9/28-15:01
 */
@Api(tags = "量表评估")
@RestController
public class RetinopathyController {

    @Autowired
    private IDtrdEntEvaluationRdRetinopathyService retinopathyService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation("添加糖尿病视网膜病变评估")
    @PostMapping(value = {"/105/23/1052301"})
    public Result<?> saveRetinopathyInfo(@RequestBody DiseaseDTO dto){
        // 参数校验
        if (dto.getIsDiagnose() == null || dto.getSymptom() == null) {
            return Result.error(STATE_PARAMS_MISSING.getCode(), STATE_PARAMS_MISSING.getMsg());
        }
        if (dto.getIsDiagnose() <= 0 || ArrayUtil.hasEmpty((Object) dto.getSymptom())) {
            return Result.error(STATE_PARAMS_ERROR.getCode(), STATE_PARAMS_ERROR.getMsg());
        }
        DiseaseEvaluationInfo info = retinopathyService.saveRetinopathyInfo(dto);
        String logContent = String.format("添加糖尿病视网膜病变评估，添加用户id：%d", dto.getPatientId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_ADD);
        return Result.OK(info);
    }

    @ApiOperation("获取最近一次的添加糖尿病视网膜病变评估记录-通过patientId")
    @GetMapping("/105/23/1052302")
    public Result<?> getRetinopathyInfoLatest(@PathParam("patientId") Integer patientId){
        if (patientId == null) {
            return Result.error(STATE_PARAMS_MISSING.getCode(), STATE_PARAMS_MISSING.getMsg());
        }
        if (patientId <= 0) {
            return Result.error(STATE_PARAMS_ERROR.getCode(), STATE_PARAMS_ERROR.getMsg());
        }
        DiseaseEvaluationInfo diseaseEvaluationInfo = retinopathyService.getRetinopathyInfoLatest(patientId);
        String logContent = String.format("获取最近一次的添加糖尿病视网膜病变评估记录，查询用户id：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(diseaseEvaluationInfo);
    }
}
